<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 店铺商品
 */
class Shopgoods extends CI_Controller
{

	function __construct()
	{
		parent::__construct();
		$this->load->model('Function_m', 'fun');
		$this->pageSize =$this->config->item('pagenum');
	}
	public function showGoodsList(){
		$nowpage = $_REQUEST["pageid"];
		if ($nowpage == "")$nowpage = 1;
		$where = "";
		$where = " AND platform_id = 2";
		//平台
		if (trim($this->input->get_post('platformID')))
		{
			$platformID = intval($this->input->get_post('platformID'));
			$this->tpl->assign("platformID", $platformID);
			$where .= " and platform_id = $platformID";
		}
		if (intval($this->input->get_post('shopID'))) {
			$shopID = intval($this->input->get_post('shopID'));
			$where .= " and  (shopid =   '" . $shopID . "') ";
			$this->tpl->assign("shopID", $shopID);
		}
		$other = trim($this->input->get_post('other'));
		if($other)
		{
			$where .= " AND ( goods_sku like '%".$other."%' or  sales_sku like '%".$other."%' or  product_id like '%".$other."%' or  goods_name like '%".$other."%')";
			$this->tpl->assign("other",$other);
		}
		$stockbegin = trim($this->input->get_post('stockbegin'));
		if($stockbegin)
		{
			$where .= " AND qty >= $stockbegin ";
			$this->tpl->assign("stockbegin",$stockbegin);
		}
		$stockend = trim($this->input->get_post('stockend'));
		if($stockend)
		{
			$where .= " AND qty <= $stockend ";
			$this->tpl->assign("stockend",$stockend);
		}
		$types = trim($this->input->get_post("types"));
		if($types){
			$where .= " AND types = $types ";
			$this->tpl->assign("types",$types);
		}else{
			$where .= " AND types = 0 ";
			$this->tpl->assign("types",0);
		}
		//$where.= "GROUP BY parent_sku ";
		$orderby = " ORDER BY id DESC ";
		$list = $this->fun->getalllist('db_amazon_productlist',$where,$nowpage,$if_csv=0,$this->pageSize,$orderby);
		$pageList = $this->pagenav->page($list["total"], $nowpage, $this->pageSize);
		$this->tpl->assign("totalpage", ceil($list["total"] / $this->pageSize));
		$this->tpl->assign("pageList", $pageList);
		$this->tpl->assign("total", $list["total"]);
		$this->tpl->assign("list",$list["list"]);
		$this->tpl->assign("d_shop", $this->fun->getdatabaseslist('d_shop', " AND del_flag= 0 and platfrom = 2 ", 'id,name', ' order by name asc'));            //查所有店铺
		$this->tpl->assign("platform", $this->fun->getdatabaseslist('db_platform'));//所有平台
		$this ->tpl->display("shopgoods/shopgoodslist.tpl");


	}
	public function saveupdate()
	{
		$id = $this->input->post('id');
		$original_price = $this->input->post('original_price');
		$price = $this->input->post('price');
		$qty = $this->input->post('qty');
		$old_qty = $this->input->post('old_qty');
		if(!$id)
		{
			die("Error!");
		}
		
		$i = 0;
		foreach($id as $key=>$rs)
		{
			$update = "";
			if($original_price[$key] != $price[$key])
			{
				$s=1;
				$update["new_price"] = $price[$key];
			}
			if($old_qty[$key] != $qty[$key])
			{
				$s=1;
				$update["new_qty"] = $qty[$key];
			}
			if($s){
				$data = serialize($update);
				 $this->db->update("db_amazon_productlist",array("updateDate"=>$data,"isupload"=>1)," id = '".$rs."'");
			}
			$s=0;
		}
		
		
	}
	public function ajaxbutton()
	{
		$op = $this->input->get_post('op');
		//获取商品数据
		if($op == "getGoodsInfo"){
			$id = trim($this->input->post("id"));
			$sql = "SELECT * FROM db_amazon_productlist WHERE id = '{$id}' ";
			$rs  = $this->db->query($sql)->row_array();
			echo json_encode($rs);exit;
		}
		//速卖通商品 价格更新
		if($op=="updAliexpressPrice"){
			$id = trim($this->input->post('id'));
			$price = trim($this->input->post('price'));
			$sql = "SELECT * FROM db_amazon_productlist WHERE id = '{$id}' ";
			$rs  = $this->db->query($sql)->row_array();
			$this->load->library('Aliexpressdata');
			$result = $this->aliexpressdata->updGoodsPrice($rs['shopid'],$price,$rs['product_id'],$rs['skuId']);
			echo $result;exit;
		}
		//速卖通商品 库存更新
		if($op=="updAliexpressStock"){
			$id = trim($this->input->post('id'));
			$stock  = trim($this->input->post('stock'));
			$sql = "SELECT * FROM db_amazon_productlist WHERE id = '{$id}' ";
			$rs  = $this->db->query($sql)->row_array();
			$this->load->library('Aliexpressdata');
			$result = $this->aliexpressdata->updGoodsStock($rs['shopid'],$stock,$rs['product_id'],$rs['skuId']);
			echo $result;exit;
		}
		if($op =="getonegoods")
		{
			$id = $this->input->post('id');
			$id =trim($id,",");
			$rsdb = $this->fun->getdatabaseslist('db_amazon_productlist'," AND id in ($id)","shopid,shopname,sales_sku,moneytype,original_price,qty,product_id,id,updateDate,isupload");
			if($rsdb)
			{
				$html="";
				foreach($rsdb as $key=>$data)
				{
					$new_price =$new_qty="";
					if($data["isupload"] == 1)
					{
						//如果还没有更新，需要将待更新的值显示，如果更新了，需要将值更新进对应的原值
						$updateDate = unserialize($data["updateDate"]);
						
						if($updateDate["new_price"])
						{
							$new_price = " 等待更新".$updateDate["new_price"];
						}
						if($updateDate["new_qty"])
						{
							$new_qty = " 等待更新".$updateDate["new_qty"];
						}
						
					}
					
					
					$html.="<tr>";
					$html.="<td>{$data['shopname']}<input name=\"id[]\" value=\"".$data['id']."\" type=\"hidden\" /><input name=\"shopid[]\" value=\"".$data['shopid']."\" type=\"hidden\" /></td><td><input name=\"sales_sku[]\" id=orderid{$data['sales_sku']} placeholder=\"销售SKU\" readonly class=\"width200\" type=\"text\" value=".$data['sales_sku']."></td><td><input name=\"original_price[]\" value=\"".$data['original_price']."\" type=\"hidden\" /><input name=\"old_qty[]\" value=\"".$data['qty']."\" type=\"hidden\" />{$data['original_price']}{$data['moneytype']}</td><td><input name=\"price[]\" id=\"price\"  value=\"{$data['original_price']}\" type=\"text\" style=\"width:50px;\">".$data['currency']." {$new_price}</td><td><input name=\"qty[]\" id=\"qty\"  value=\"{$data['qty']}\" type=\"text\" style=\"width:50px;\">{$new_qty}</td>";
					$html.="</tr>";
				}
			}
			echo $html;
			die;
		}


	}


}

?>